﻿using MyUAMS.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Net.Cache;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace MyUAMS.BLL
{
    public class WorkServer
    {
        DataClasses1DataContext db = new DataClasses1DataContext();

        public bool IsNameExist(string name)
        {
            Asset_Table worker = (from c in db.Asset_Table
                                   where c.Aname == name
                                   select c).FirstOrDefault();
            if (worker != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool GetInformationByUid(int uid)
        {
                AP_Table apinformate = (from c in db.AP_Table
                                        where (c.Uid == uid) && (c.Status != "审核完成")
                                        select c).FirstOrDefault();
                if (apinformate != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }
        public List<AP_Table> GetMyBR(int uid)
        {
            return (from c in db.AP_Table
                    where c.Uid == uid && c.Status != "审核完成" && (c.APname == "出库" || c.APname == "入库")
                    select c).ToList();
        }
        public AP_Table GetAPByAPid(int uid)
        {
            return(from c in db.AP_Table
                   where c.APid==uid
                   select c).First();
        }
        public void DeleteAPByAPid(int apid)
        {
            if (apid != 0)
            {
                var APtable = (from c in db.AP_Table
                               where c.APid == apid
                               select c).First();
                db.AP_Table.DeleteOnSubmit(APtable);
                db.SubmitChanges();
            }
        }
        public void Insert(string name, string unit, string describe, int tid, string cperson,decimal telephone, string department, decimal dtelephone)
        {
            Asset_Table worker = new Asset_Table
            {
                Aname = name,
                Aamount = 0,
                Unit = unit,
                Describe = describe,
                Tid = tid,
                Department = department,
                Telephone = telephone,
                Cperson = cperson,
                DTelephone = dtelephone
                
            };
            db.Asset_Table.InsertOnSubmit(worker);
            db.SubmitChanges();
        }
        public void DeletePro(int assetId)
        {
            if (assetId != 0)
            {
                var product = (from p in db.Asset_Table
                               where p.Aid == assetId
                               select p).First();
                db.Asset_Table.DeleteOnSubmit(product);
                db.SubmitChanges();
            }
            else
            {
                
            }
        }
        public List<Asset_Table> GetAllProduct()
        {
            return (from p in db.Asset_Table
                    select p).ToList();
        }
        public Asset_Table GetAssetByAid(int assestId)
        {
            return (from p in db.Asset_Table
                    where p.Aid == assestId
                    select p).First();
        }
        public List<AssetType_Table> GetAllCategory()
        {
            return (from c in db.AssetType_Table
                    select c).ToList();
        }
        public void Update(int aid, int tid, string name, string unit, string describe, string cperson, decimal telephone, string department, decimal dtelephone)
        {
            Asset_Table asset = (from p in db.Asset_Table 
                                 where p.Aid == aid 
                                 select p).First();
            asset.Tid = tid;
            asset.Aname = name;
            asset.Unit = unit;
            asset.Describe = describe;
            asset.Cperson = cperson;
            asset.Telephone = telephone;
            asset.Department = department;
            asset.DTelephone = dtelephone;

            db.SubmitChanges();
        }
        public List<Asset_Table> Search(string search)
        {
            return(from p in db.Asset_Table
                   where SqlMethods.Like(p.Aname,"%"+search+"%")
                   select p).ToList();
        }
        public void Insert_AP(int aid,string aname,string apname,int Vid,int amount,int uid,string uname,string remark)
        {
            AP_Table ap = new AP_Table()
            {
                Aid = aid,
                Aname = aname,
                APname = apname,
                VoucherId = Vid,
                Aamount = amount,
                Uid = uid,
                Uname = uname,
                Step = 1,
                Status = "一审中",
                Remark = remark,
                Date = DateTime.Now
            };
            db.AP_Table.InsertOnSubmit(ap);
            db.SubmitChanges();
        }
        public void Insert_BR(int aid,string aname,string apname,int uid,string uname,string buname,decimal bphone,string runame,decimal rphone,int bamount,string remark)
        {
            // 首先插入 AP_Table 并获取生成的 APid
            AP_Table ap = new AP_Table()
            {
                Aid = aid,
                Aname = aname,
                APname = apname,
                VoucherId = 01,
                Uid = uid,
                Uname = uname,
                Aamount = bamount,
                Step = 1,
                Status = "一审中",
                Remark = remark,
                Date = DateTime.Now
            };
            db.AP_Table.InsertOnSubmit(ap);
            db.SubmitChanges(); // 提交更改，这将导致 APid 被数据库自动生成并赋值给 ap 对象

            // 现在插入 BR_Table 并使用 ap 对象中的 APid
            BR_Table br = new BR_Table()
            {
                APid = ap.APid, // 使用刚才插入的 AP_Table 的 APid
                Aid = aid,
                Uid = uid,
                Uname = uname,
                Buname = buname,
                Btelephone = bphone,
                Runame = runame,
                Rtelephone = rphone,
                Bamount = bamount,
                Bdate = DateTime.Now,
                Status = "审核中"
            };
            db.BR_Table.InsertOnSubmit(br);
            db.SubmitChanges(); // 再次提交更改以插入 BR_Table
        }
        public List<BR_Table> GetBRByUid(int uid)
        {
            return(from c in  db.BR_Table
                   where c.Uid == uid
                   select c).ToList();
        }
        public void UpdateAPInBRByUid(int apid,int rid,int ramount)
        {
            AP_Table ap = (from c in db.AP_Table
                           where c.APid == apid
                           select c).First();
            if(ap != null)
            {
                ap.Ramount = ramount;
                ap.APname = "还入";
                ap.APuid = null;
                ap.APuname = null;
                ap.Onedate = null;
                ap.Twedate = null;
                ap.Threedate = null;
                ap.Step = 1;
                ap.Status = "一审中";
                ap.Date = DateTime.Now;
               
            }
            BR_Table br =(from p in db.BR_Table
                          where p.Rid==rid
                          select p).First();
            if (br != null)
            {
                br.Status = "审核中";
                br.Rdate = DateTime.Now;
                br.Ramount = ramount;
            }

            db.SubmitChanges();
        }
        
        
    }
}
